#ifndef __SLEEPY_HPP__
#define __SLEEPY_HPP__

/**
 *  This file is part of PennerBot
 *
 *  Copyright (C) 2010 microkernel
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <iostream>

#include <cstdlib>
#include <ctime>

#include <core/random.hpp>

#include <boost/thread/recursive_mutex.hpp>

namespace PBot
{
namespace Core
{

class Sleepy
{
public:
    Sleepy()
     : sleep(0), newTime(0), oldTime(0)
    {
    }

    virtual ~Sleepy()
    {
    }

    time_t Sleep(unsigned int sec)
    {
        sleep = sec;
        newTime = std::time(0);
        return sleep;
    }

    time_t Sleep(unsigned int lsec, unsigned int hsec)
    {
        sleep = random(lsec, hsec);

        newTime = std::time(0);
        return sleep;
    }

    bool IsAwake()
    {
        oldTime = newTime;
        newTime = std::time(0);

        sleep -= newTime-oldTime;

        if(sleep <= 0)
        {
            sleep = 0;
            return true;
        }
        else
        {
            return false;
        }
    }

protected:
    Random random;

private:
    time_t sleep;
    time_t newTime;
    time_t oldTime;
};

}
}

#endif // __SLEEPY_HPP__
